package com.example.lj.uav.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.example.lj.uav.R;

public class DragRectView extends android.support.v7.widget.AppCompatImageView {

    private Context context;
    private int width, height, marginTop, marginLeft, parentLeft, parentTop;
    private int rectWidth;

    public DragRectView(Context context) {
        this(context, null);
    }

    public DragRectView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragRectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        if (wm != null)
            wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;         // 屏幕宽度（像素）
        height = dm.heightPixels;       // 屏幕高度（像素）
    }


    public void initRect(int left, int right, int top, int bottom, int parentLeft, int parentTop) {
        marginLeft = left;
        marginTop = top;
        width = right - left;
        height = bottom - top;
        this.parentLeft = parentLeft;
        this.parentTop = parentTop;
        rectWidth = marginLeft + width * 5 / 31;
        invalidate();
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setColor(ContextCompat.getColor(context, R.color.dodgerblue));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);

        @SuppressLint("DrawAllocation")
        RectF r = new RectF();
        r.left = marginLeft;
        r.right = rectWidth;
        r.top = marginTop;
        r.bottom = marginTop + height;
        canvas.drawRect(r, paint);

    }

    private float orX, offX;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                orX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                offX = event.getRawX() - orX - parentLeft;
                offX = offX > 0 ? offX : 0;
                offX = offX < width - rectWidth ? offX : width - rectWidth;
                ((View) getParent()).scrollTo((int) -offX, 0);
                break;
            case MotionEvent.ACTION_UP:
                upListener.onActionUp(offX / (width - rectWidth));
                break;
        }
        return true;
    }

    private OnActionUpListener upListener;

    public interface OnActionUpListener {
        void onActionUp(float p);
    }

    public void setOnActionUpListener(OnActionUpListener cb) {
        upListener = cb;
    }

    public int dp2px(float dp) {
        final float scale = context.getResources().getDisplayMetrics().density; //当前屏幕密度因子
        return (int) (dp * scale + 0.5f);
    }

    public int px2dp(float px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

}